#include "widget_resource.h"

//=====================================================================
//              Class to store Photon resource
//=====================================================================
//
static PyTypeObject WidgetResourceType = 
{
  PyObject_HEAD_INIT(NULL)
  0,                         // ob_size 
  "ph.WidgetResource",            // tp_name
  sizeof(WidgetResource),         // tp_basicsize 
  0,                         // tp_itemsize
  (destructor)WidgetResource_dealloc,// tp_dealloc
  0,                         // tp_print 
  0,                         // tp_getattr 
  0,                         // tp_setattr
  0,                         // tp_compare 
  0,                         // tp_repr
  0,                         // tp_as_number
  0,                         // tp_as_sequence 
  0,                         // tp_as_mapping 
  0,                         // tp_hash 
  0,                         // tp_call
  0,                         // tp_str
  0,                         // tp_getattro
  0,                         // tp_setattro
  0,                         // tp_as_buffer
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags 
  "Photon wrapper objects",  // tp_doc 
  0,                         // tp_traverse 
  0,                         // tp_clear 
  0,                         // tp_richcompare 
  0,                         // tp_weaklistoffset 
  0,                         // tp_iter 
  0,                         // tp_iternext 
  WidgetResource_methods,         // tp_methods 
  WidgetResource_members,         // tp_members
  0,                         // tp_getset 
  0,                         // tp_base 
  0,                         // tp_dict
  0,                         // tp_descr_get 
  0,                         // tp_descr_set 
  0,                         // tp_dictoffset 
  (initproc)WidgetResource_init,  // tp_init
  0,                         // tp_alloc 
  WidgetResource_new              // tp_new 
};

static void WidgetResource_dealloc (WidgetResource* self)
{
  self->ob_type->tp_free ((PyObject*) self);
}

static PyObject* WidgetResource_new (PyTypeObject* type, PyObject* args)
{

#if DEBUG_LEVEL == HIGH
  MARK ("WidgetResource_new")
#endif

  WidgetResource* self;

  self = (WidgetResource*) type->tp_alloc (type, 0);
  return (PyObject*) self; 
}

static PyObject* WidgetResource_get (WidgetResource* self)
{
  PyObject* result;

#if DEBUG_LEVEL == HIGH
  if (self->resource && self->type == eText)
    REPORT ("%s", self->resource);
#endif

  switch (self->type)
  {
    case eText:
      result = PyString_FromString (self->resource);
      break;
      
    case eNone:
    default:
      Py_INCREF (Py_None);
      result = Py_None;
      break;
  }

  if (!result)
  {
    PyErr_SetString (PyExc_RuntimeError, 
        "Resource type undefined");
  }

  return result;
}

static int WidgetResource_init (WidgetResource* self)
{
  self->type = eNone;
  self->resource = NULL;

#if DEBUG_LEVEL == HIGH
    REPORT ("%d", self->type);
    REPORT ("%x", &self->resource);
#endif

  return 0;
}

static PyMemberDef  WidgetResource_members [] =
{
  {NULL}  // Sentinel 
};

static PyMethodDef  WidgetResource_methods [] =
{
  {
    "get", (PyCFunction) WidgetResource_get, METH_NOARGS,
    "to get Python object corresponding with resource"
  },
  {NULL}  // Sentinel 
};

//=====================================================================
//                Module declaration functions
//=====================================================================

static PyMethodDef module_methods [] = 
{
  {NULL, NULL, 0, NULL}
};

INIT_FUNC (ph_api_widget_resource, WidgetResource)
